#!/bin/zsh
# Usage: git prev [number]

if [ -z "$1" ]; then
  number=1
else
  number=$1
fi

awkProg="
  BEGIN { FS=\":sep:\" } {
    if ( last != \$1 ) {
      last=\$1
      if ( ${number} == counter++ ) {
        res=\$2
        next
      }
    }
  } END { print res }"
branch="$(git symbolic-ref refs/remotes/origin/HEAD)"
head="$(git rev-parse HEAD)"
pretty="%s:sep:%H"

git log --pretty="${pretty}" ${branch} | \
  grep -A"$((number*10))" ${head} | \
  awk ${awkProg} | \
  xargs git checkout
